//
//  HomeBizMenusCustomView.m
//  YYZB
//
//  Created by Tongqingqing on 2017/9/27.
//
//

#import "HomeBizMenusCustomView.h"
#import <Masonry/Masonry.h>
#import "UIImageView+OnlineImage.h"
#import "UIColor+Hex.h"

@implementation HomeBizMenusCustomView

- (instancetype)initWithModel:(BizMenu *)model
{
    if (self = [super init]) {
        
        UITapGestureRecognizer *single = [[UITapGestureRecognizer alloc]
                                          initWithTarget:self
                                          action:@selector(click:)];
        single.numberOfTapsRequired = 1;
        single.numberOfTouchesRequired = 1;
        [self addGestureRecognizer:single];
    }
    return self;
}

- (void)layoutSubview
{
    [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(self);
    }];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(self);
        make.height.equalTo(@30);
    }];
}

- (void)bindData:(BizMenu *)data
{
    self.dataModel = data;
    [self addSubview:self.bgImgView];
    [self addSubview:self.titleLab];
    [self layoutSubview];
    self.titleLab.text = data.ModuleName;
     if ([data.ConfigType integerValue] == 1) {
        self.bgImgView.hidden = YES;
        if(!data.Background){
            self.titleLab.textColor = [UIColor blackColor];
            self.backgroundColor = [UIColor whiteColor];
        } else {
            self.titleLab.textColor = [UIColor whiteColor];
            self.backgroundColor = [UIColor colorWithHex:data.Background];
            if([data.Background isEqualToString:@"#ffffff"] || [data.Background isEqualToString:@"#FFFFFF"]){
                self.titleLab.textColor = [UIColor blackColor];
            }
        }
        
    } else  if ([data.ConfigType integerValue] == 2) {
        self.bgImgView.hidden = NO;
        [self.bgImgView setOnlineImage:data.FullImgPath];
        NSLog(@"~~~~~~%@",data.FullImgPath);
        if ([data.FullImgPath isEqual:@""]) {
            self.bgImgView.backgroundColor = [UIColor whiteColor];
        }
    }
}

- (void)click:(UITapGestureRecognizer *)tap
{
    if (_delegate &&
        [self.delegate respondsToSelector:@selector(selfClickWithModel:withIndex:)]) {
        [_delegate selfClickWithModel:self.dataModel withIndex:self.tag / 100];
    }
}

#pragma getter and setter
- (UILabel *)titleLab
{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor whiteColor];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.font = [UIFont systemFontOfSize:14];
    }
    return _titleLab;
}

- (UIImageView *)bgImgView
{
    if (!_bgImgView) {
        _bgImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"white"]];
        _bgImgView.backgroundColor = [UIColor clearColor];
//        self.backgroundColor = [UIColor whiteColor];
    }
    return _bgImgView;
}

@end
